/*
 * PackageItemResource.cpp
 *
 *  Created on: 2014. 2. 27.
 *      Author: the2
 */

#include "PackageItemResource.h"
#include <Database/Static.h>
#include "../NUtil/Pool.h"
#include "../NUtil/Utils.h"

namespace ProjectZ { namespace NResource {

PackageItemData::~PackageItemData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
PackageItemData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool PackageItemData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(" SELECT * FROM resource_package_item where company = %d and sale_cd = %d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		PackageItemData::Flyweight * pFly = new PackageItemData::Flyweight;

		int packageIdx = 0, tmpMaxCount, tmpCompany, tmpSaleCode;
		clsAuto->GetRecord() >> NATIONAL_CODE >> packageIdx >> tmpMaxCount >> tmpCompany >> tmpSaleCode
				>> pFly->_ITEM1_TID >> pFly->_ITEM1_COUNT
				>> pFly->_ITEM2_TID >> pFly->_ITEM2_COUNT
				>> pFly->_ITEM3_TID >> pFly->_ITEM3_COUNT
				>> pFly->_ITEM4_TID >> pFly->_ITEM4_COUNT;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[packageIdx] = pFly;
	}

	return true;
}

PackageItemData::Flyweight * PackageItemData::GetFly(int packageIdx)
{
	auto iter = clsFlyTree.find(packageIdx);

	if ( iter == clsFlyTree.end() )
		return NULL;
	else
		return iter->second;
}

PackageItemResource::PackageItemResource()
{
	pPackageItemAD = new PackageItemData;
	pPackageItemIOS = new PackageItemData;
}

PackageItemResource::~PackageItemResource()
{
	delete pPackageItemAD;
	delete pPackageItemIOS;
}

bool PackageItemResource::LoadResource()
{
	if (false == pPackageItemAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pPackageItemIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

PackageItemData::Flyweight * PackageItemResource::GetFly(int company, int saleCode, int packageIdx)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pPackageItemAD->GetFly(packageIdx);

	case NUtil::MarketType::IOS_KAKAO:
		return pPackageItemIOS->GetFly(packageIdx);

	default:
		return NULL;
	}

	return NULL;
}

} /* namespace NResource */ } /* namespace ProjectZ */
